package com.zhuss.car.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.common.Processor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.car.driver.SearchDriverBo;
import com.zhuss.bean.vo.car.driver.DetailDriverVo;
import com.zhuss.bean.vo.car.driver.ListDriverVo;
import com.zhuss.car.db.dao.ext.DriverDao;
import com.zhuss.car.db.entity.Driver;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.CompanyDao;
import com.zhuss.core.db.entity.Company;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;


/**
 * @description: 司机读
 * @author: zhushanshuo
 * @create: Mon Dec 21 13:31:45 CST 2020
 **/
@Component("driverRead")
public class DriverRead {

	@Autowired
	private DriverDao driverDao;

	@Autowired
	private CompanyDao companyDao;

	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;


	public Map<String, Driver> listMap() {
		List<Driver> list = driverDao.listAll();
		Map<String, Driver> map = new HashMap<>();
		for (Driver c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}

	/**
	 * 司机
	 * @return
	 */
	public List<Driver> listDriver() {
		List<Driver> result = driverDao.listAll();
		return result;
	}

	/**
	 * 司机
	 * @return
	 */
	public Object listDriverSelect() {
		List<Driver> driverList = listDriver();
		List<ListVo> result = new ArrayList<>();
		transferList(driverList, result);
		return result;
	}


	/**
	 * 司机
	 * @return
	 */
	public Object listDriver(List<Long> driverIdList) {
		List<Driver> result = driverDao.listById(driverIdList);
		return result;
	}


	/**
	 * 根据编号查询司机
	 * @param
	 * @return
	 */
	public List<Driver> list(String driverNo) {
		List<Driver> result = driverDao.listByName(driverNo);
		return result;
	}

	public List<Driver> listNotId(String name, Long driverId) {
		List<Driver> result = new ArrayList<Driver>();
		List<Driver> list = list(name);
		for (Driver c : list) {
			if (!c.getDriverId().equals(driverId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}

	/**
	 * 司机
	 * @return
	 */
	public Object listDriverId() {
		List<Driver> driverList = driverDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(driverList, result);
		return result;
	}

	private void transferListId(List<Driver> driverList, List<Long> result) {
		for (Driver item : driverList) {
			result.add(item.getDriverId());
		}
	}

	private void transferList(List<Driver> driverList, List<ListVo> result) {
		for (Driver item : driverList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getDriverId());
			vo.setLabel(item.getName());
			result.add(vo);
		}

	}

	/**
	 * 司机列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchDriverBo bo) throws DzcxException {
		PageVo<ListDriverVo> result = new PageVo<ListDriverVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
		//公司id映射成英文
		List<ListDriverVo> driverVoList =  driverDao.list(bo);
		Map<Long,String> map = new HashMap<>();
		List<Company> companyList = companyDao.listAll();
		companyList.forEach(company -> {
			map.put(company.getCompanyId(), company.getName());
		});

		for (ListDriverVo vo : driverVoList) {
			if (vo.getCompanyId() != null) {
				vo.setCompanyName(map.get(vo.getCompanyId()));
			}
			vo.setPicUrl(genUrl(vo.getPic()));
		}

        result.setItems(driverVoList);
        result.setTotal(driverDao.countList(bo));
		return result;
	}

	/**
	 * 司机详情
	 * @param driverId
	 * @return
	 * @throws DzcxException
	 */
	public DetailDriverVo detail(Long driverId) throws DzcxException {
		DetailDriverVo result = driverDao.detail(driverId);
		if (result == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		} else {
			if (result.getCompanyId() != null) {
				Map<Long,String> map = new HashMap<>();
				List<Company> companyList = companyDao.listAll();
				companyList.forEach(company -> {
					map.put(company.getCompanyId(), company.getName());
				});
				result.setCompanyName(map.get(result.getCompanyId()));
			}
			result.setPicUrl(genUrl(result.getPic()));
		}
		return result;
	}

	private String genUrl(String img) throws DzcxException {
		if (img == null || "".equals(img.trim())) {
			return null;
		}
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKey(img);
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		return downloadVo.getUrl();
	}

	public Object select(SearchDriverBo bo) throws DzcxException {
		if (bo.getCompanyId() == null || bo.getCompanyId() <=0l) {
			throw new DzcxException(ExceptionCode.公司ID为空);
		}
		return driverDao.selectByCompanyId(bo.getCompanyId());
	}



}
